package com.orangelife.mobile.pay.biz;
import android.os.Handler;
import android.os.Message;

import com.common.sp.PaySp;
import com.curry.android.http.HttpUtil;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.FastJsonTools;
import com.curry.orangelife.mobile.wxapi.WXPayEntryActivity;
import com.orangelife.mobile.app.application.ExitApplication;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.confirmOrder.entity.PaidInfo;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.individual.entity.WeChat;
import com.orangelife.mobile.pay.interfaze.IWeChat;
import com.orangelife.mobile.pay.interfaze.IWeChatCallBack;
import com.tencent.mm.sdk.modelbase.BaseResp;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/1/18.
 */
public class ThridPayKeyRequest extends WXPayEntryActivity implements IWeChatCallBack {
    protected PayFactory payFactory;
    private static ThridPayKeyRequest thridPayKeyRequest;
    private String orderInfo,outtradeno,totalCost;
    private HashMap<String,PaidInfo> paidInfoMap;
    public static ThridPayKeyRequest getInstance(){
        if(thridPayKeyRequest==null){
            thridPayKeyRequest = new ThridPayKeyRequest();
        }
        return thridPayKeyRequest;
    }

    protected void initParams(String orderInfo,String outtradeno,String totalCost){
        this.orderInfo = orderInfo;
        this.outtradeno = outtradeno;
        this.totalCost = totalCost;
    }
    protected void getApilayFromServer(Handler handler) {
        String url = Constant.URL_APILAY_KEY;
        Map<String, Object> rootMap = new HashMap<String, Object>();
        rootMap.put("url", url);
        rootMap.put("wat", PaymentModule.APILAY_KEY);
        JSONRequest.getInstance().sendRequest(JSONRequest.GET, rootMap, handler, HttpUtil.ACTION_URI);
    }

    protected void getNewApilayFromServer(Handler handler) {
        String url = Constant.URL_NEW_APILAY_KEY;
        HashMap rootMap = new HashMap<String, String>();
//        try{
//            rootMap.put("url",url);
            rootMap.put("goodsName", "钱包充值");
            rootMap.put("outTradeNo", outtradeno);
//        }catch (UnsupportedEncodingException e){
//            e.printStackTrace();
//        }

//        JSONRequest.getInstance().sendRequest(JSONRequest.GET, rootMap, handler, HttpUtil.ACTION_URI);
        VolleyHttpUtil.getInstance().getWithHandler(url,handler,PaymentModule.APILAY_KEY,rootMap);
    }

    protected void getWeChatFromServer(Handler handler){
        String url = Constant.URL_WECHAT;
        Map<String,Object> mapParams = new HashMap<>();
        mapParams.put("body",orderInfo);
        mapParams.put("out_trade_no",outtradeno);
        mapParams.put("total_fee",totalCost);
        mapParams.put("trade_type","APP");
        mapParams.put("detail",orderInfo);
        JSONRequest.getInstance().sendRequest(JSONRequest.GET, url, mapParams, handler, PaymentModule.WECHAT_KEY, HttpUtil.ACTION_URI);
    }

    protected JSONObject getApilayJson(JSONObject jsonObject) throws JSONException {
        jsonObject.put("outtradeno", outtradeno);
        jsonObject.put("totalCost", totalCost);
        jsonObject.put("goodsName", "钱包充值");
        return jsonObject;
    }

    protected void getWeChatJson(Message msg){
        WeChat weChat =  FastJsonTools.getBean(msg.obj.toString(), WeChat.class);
        if(weChat!=null) {
            weChat.setBody(orderInfo);
            weChat.setOutTradeNo(outtradeno);
            weChat.setTotalFee(totalCost);
            PaySp.setWxAppID(weChat.getAppID());
            weChatCallBack(weChat);
        }
    }

    /**
     * 微信支付回调方法
     */
    protected void weChatCallBack(WeChat wechat){
        callBack(this);
        initpayFactory();
        IWeChat iPayManage = payFactory.produce("wechat", null);
        iPayManage.setParameter(this, wechat);
    }

    @Override
    public void getResp(BaseResp resp) {
    }

    /**
     * 实例化支付工厂类
     */
    protected void initpayFactory(){
        if(payFactory==null){
            payFactory = new PayFactory();
        }
    }

    protected void putPaidInfo(PaidInfo paidInfo){
        PaySp.setPaidInfo(paidInfo);
        ExitApplication.getInstance().addMainActivity(this);
    }
}
